package com.ddwl.user.service;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.Constant;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.feign.api.GateApi;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.schema.bo.user.EditUserAddrBo;
import com.ddwl.schema.vo.gate.RegionVo;
import com.ddwl.schema.vo.user.UserAddrVo;
import com.ddwl.user.dao.UserAddrDao;
import com.ddwl.user.dao.model.UserAddr;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/6/14 9:31
 */
@Service
public class UserAddrService {

    @Resource
    private UserAddrDao userAddrDao;
    @Resource
    private GateApi gateApi;

    @Transactional(rollbackFor = Throwable.class)
    public void save(EditUserAddrBo bo) {
        RegionVo regionVo = gateApi.geoRegion(bo.getRegionId()).getResult();
        ParamException.isNull(regionVo);
        if(BaseEnum.StatusEnum.ENABLE.getCode().equals(bo.getDefaultFlag())){
            userAddrDao.removeDefault(bo.getUserId());
        }
        //新增
        if (!StringUtils.hasText(bo.getId())) {
            UserAddr inset = BeanConverUtils.convertBean(bo,UserAddr.class);
            inset.setProvinceName(regionVo.getProvinceName());
            inset.setCityName(regionVo.getCityName());
            inset.setDistrictName(regionVo.getDistrictName());
            userAddrDao.insert(inset);
        } else {
            UserAddr userAddr = userAddrDao.selectByPrimaryKey(bo.getId());
            ParamException.isNull(userAddr);
            UserAddr entity = BeanConverUtils.convertBean(bo,UserAddr.class);
            entity.setProvinceName(regionVo.getProvinceName());
            entity.setCityName(regionVo.getCityName());
            entity.setDistrictName(regionVo.getDistrictName());
            userAddrDao.updateByPrimaryKeySelective(entity);
        }
    }


    public List<UserAddrVo> getList(String userId) {
        UserAddr userAddr = new UserAddr();
        userAddr.setUserId(userId);
        userAddrDao.select(userAddr);
        return BeanConverUtils.convertBeanList(userAddrDao.select(userAddr),UserAddrVo.class);
    }


    public UserAddrVo getById(String id) {
        return BeanConverUtils.convertBean(userAddrDao.selectByPrimaryKey(id),UserAddrVo.class);
    }

    @Transactional(rollbackFor = Throwable.class)
    public void setDefault(String userId, String id) {
        userAddrDao.removeDefault(userId);
        UserAddr userAddr = new UserAddr();
        userAddr.setId(id);
        userAddr.setDefaultFlag(BaseEnum.StatusEnum.ENABLE.getCode());
        userAddrDao.updateByPrimaryKeySelective(userAddr);
    }

    public UserAddrVo getDefault(String userId) {
        UserAddr userAddr = new UserAddr();
        userAddr.setUserId(userId);
        userAddr.setDefaultFlag(BaseEnum.StatusEnum.ENABLE.getCode());
        return BeanConverUtils.convertBean(userAddrDao.selectOne(userAddr),UserAddrVo.class);
    }

    @Transactional(rollbackFor = Throwable.class)
    public void delete(String id) {
        userAddrDao.deleteById(id);
    }
}
